import { createRouter, createWebHistory , Router} from 'vue-router'
import store from "@/store/index"
import {RouterConfig,loginPath,createMyRoutes,createDynamicRoutes} from "../config/router.config"

interface MyRouter extends Router {
  $rebuild?:() => void;
  $goLogin?:() => void;
  matcher?:any;
}

// const routes: Array<RouteRecordRaw> = [
//   {
//     path: '/',
//     name: 'Home',
//     component: Home
//   },
//   {
//     path: '/about',
//     name: 'About',
//     // route level code-splitting
//     // this generates a separate chunk (about.[hash].js) for this route
//     // which is lazy-loaded when the route is visited.
//     component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
//   }
// ]

const createRoutes = (routes:any[]) :MyRouter => createRouter({...RouterConfig,history: createWebHistory(process.env.BASE_URL),routes})

const router = createRoutes(createMyRoutes(store.getters.routes))

//
router.$goLogin = function(){
  router.push(loginPath)
}
//
router.$rebuild = function (this:any) {
  const routes = createDynamicRoutes(store.getters.routes)
  // const nr = createRoutes(routes);
  // this.matcher = nr.matcher;
  routes.forEach(r => router.addRoute(r))
}

// 路由守护
router.beforeEach((to, from, next) => {
  if (to.path === from.path) {
      return;
  }
  next();
})

export default router
